#include "Hud.h"
#include "Player.h"
#include "ImgMgr.h"

Hud::Hud()
{

}

void Hud::init(ImgMgr *imageManager)
{
    m_heart = imageManager->getSprite(9);
    m_heart.setScale(2, 2);
    m_font.loadFromFile("ressource/font.ttf");
    m_energy.setColor(sf::Color::White);
    m_energy.setCharacterSize(32);
    m_energy.setFont(m_font);
    m_gameOver.setColor(sf::Color::Red);
    m_gameOver.setCharacterSize(96);
    m_gameOver.setFont(m_font);
    m_gameOver.setString("Game Over");
}

void Hud::render(sf::RenderWindow *app, Player *player, sf::View *view)
{
    float energy = player->getEnergy() * 100.0 / 10;
    std::string stringEnergy;
    std::ostringstream oss;
    oss << energy;
    stringEnergy = oss.str();
    stringEnergy += "%";
    m_energy.setString(stringEnergy);
    m_energy.setColor(sf::Color::Color(67, 67, 67));
    m_energy.setPosition(view->getCenter().x - view->getSize().x / 2 + 6, view->getCenter().y - view->getSize().y / 2 + m_energy.getGlobalBounds().height + 2);
    app->draw(m_energy);
    m_energy.setColor(sf::Color::Blue);
    m_energy.setPosition(view->getCenter().x - view->getSize().x / 2 + 4, view->getCenter().y - view->getSize().y / 2 + m_energy.getGlobalBounds().height);
    app->draw(m_energy);

    int e = 0;
    int px = 0;
    while(e < player->getHeart())
    {
        m_heart.setPosition(view->getCenter().x - view->getSize().x / 2 + px, view->getCenter().y - view->getSize().y / 2);
        app->draw(m_heart);
        e++;
        px += 32;
    }
    if(player->getHeart() <= 0)
    {
        m_gameOver.setColor(sf::Color::Color(67, 67, 67));
        m_gameOver.setPosition(view->getCenter().x - (m_gameOver.getGlobalBounds().width / 2) + 2, view->getCenter().y - (m_gameOver.getGlobalBounds().height) + 2);
        app->draw(m_gameOver);
        m_gameOver.setColor(sf::Color::Red);
        m_gameOver.setPosition(view->getCenter().x - (m_gameOver.getGlobalBounds().width / 2), view->getCenter().y - (m_gameOver.getGlobalBounds().height));
        app->draw(m_gameOver);
    }
}
